﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHunspell;
using EmailCenter_SEK53.Models;
using EmailCenter_SEK53.Models.Objects;

namespace EmailCenter_SEK53.Views
{
    public partial class frmSendEmail : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public IRepository _repository;
        private static Hunspell spellChecker;
        private static bool isExistRisk = true;

        public frmSendEmail(IRepository repository)
        {
            _repository = repository;
        }

        public frmSendEmail()
            : this(new Repository())
        {
            InitializeComponent();
            spellChecker = new Hunspell(@".\Dictionaries\en_GB\en_GB.aff", @".\Dictionaries\en_GB\en_GB.dic");

            Timer spellCheckTimer = new Timer();
            spellCheckTimer.Interval = 3000;
            spellCheckTimer.Tick += new EventHandler(spellCheckTimer_Tick);
            spellCheckTimer.Start();

            rtbMailMessage.TextChanged += new EventHandler(rtbMailMessage_TextChanged);

            AutoCompleteStringCollection emailData = new AutoCompleteStringCollection();
            AutoCompleteSource acs = new AutoCompleteSource();
            
            List<SuggestEmailItem> suggestEmailList = _repository.GetSuggestEmail().ToList();
            foreach (SuggestEmailItem sei in suggestEmailList)
            {
                emailData.Add(String.Format("{0}<{1}>", sei.Name, sei.Email));
                emailData.Add(sei.Email);
            }
            txtTo.AutoCompleteCustomSource = emailData;
        }

        void rtbMailMessage_TextChanged(object sender, EventArgs e)
        {
            isExistRisk = true;
        }

        void spellCheckTimer_Tick(object sender, EventArgs e)
        {
            if (isExistRisk)
            {
                this.Reset();
                string currentMessage = rtbMailMessage.Text;
                string[] wordArray = currentMessage.Split(new Char[] { ' ', ',', '.', ':', '\t', '?', '>', '<', '=', '{', '}' });
                for (int i = 0; i < wordArray.Length; i++)
                {
                    if (spellChecker.Spell(wordArray[i]) == false)
                    {
                        this.HighlightPhrase(wordArray[i], Color.Red);
                    }
                }
                rtbMailMessage.SelectionStart = rtbMailMessage.Text.Length;
                rtbMailMessage.SelectionLength = 0;
                isExistRisk = false;
            }
        }

        private void Reset()
        {
            rtbMailMessage.SelectionStart = 0;
            rtbMailMessage.SelectionLength = rtbMailMessage.Text.Length;
            rtbMailMessage.SelectionColor = Color.Black;
            rtbMailMessage.SelectionStart = rtbMailMessage.Text.Length;
            rtbMailMessage.SelectionLength = 0;
        }

        private void HighlightPhrase(string phrase, Color color)
        {
            int i,j;
            string s = rtbMailMessage.Text;
            for (i = 0; ; )
            {
                j = s.IndexOf(phrase, i, StringComparison.CurrentCultureIgnoreCase);
                if (j < 0) break;
                rtbMailMessage.SelectionStart = j;
                rtbMailMessage.SelectionLength = phrase.Length;
                rtbMailMessage.SelectionColor = color;
                i = j + 1;
            }
        }
    }
}
